﻿Imports OutlookStyleControls
Imports System.Collections
Imports System.ComponentModel

Public Class frmHopDong
    Public bCallForm As Boolean = False 'Cho biet la form nay dc goi tu form khac
    Public isInsert As Boolean = True
    Public selectedHD As CHopDong = Nothing
    Private mSelectedIndex As Integer = 0

#Region "Draw ListView"
    Private Sub lvDMPhieu_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs) Handles lvDMPhieu.DrawColumnHeader
        Dim sf As New StringFormat()
        Try
            sf.Alignment = StringAlignment.Center
            ' Draw the standard header background.
            e.DrawBackground()
            ' Draw the header text.
            Dim headerFont As New Font("Courier New", 9, FontStyle.Bold)
            e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf)
        Catch ex As Exception
        Finally
            sf.Dispose()
        End Try
    End Sub

    Private Sub lvDMPhieu_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles lvDMPhieu.DrawSubItem
        Dim brText As Brush
        e.DrawBackground()
        Try
            'Neu la Selected Item thi to mau Red, nguoc lai to theo BCTH
            If (e.ItemState And DrawItemState.Selected) = DrawItemState.Selected Then
                brText = Brushes.Red
            Else
                brText = Brushes.Black
            End If
            ' Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text, Me.lvDMPhieu.Font, brText, e.Bounds)
        Catch ex As Exception

        End Try
    End Sub
#End Region

    Private Sub lvDMPhieu_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvDMPhieu.DoubleClick
        If bCallForm Then
            For Each item As ListViewItem In Me.lvDMPhieu.SelectedItems
                selectedHD = CHopDongs.GetHopDongBySoHD(item.SubItems(0).Text)
            Next
            Me.Close()
        End If
    End Sub

    Private Sub lvDMPhieu_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvDMPhieu.SelectedIndexChanged
        For Each item As ListViewItem In Me.lvDMPhieu.SelectedItems
            mSelectedIndex = item.Index
            Dim objHD = CHopDongs.GetHopDongBySoHD(item.SubItems(0).Text)
            FormatGridStyleGridDMPhieu(objHD.HOPDONG_ID)
        Next
    End Sub

    Public Sub Init()
        Dim arrDMHopDong As ArrayList = CHopDongs.GetHopDongList()
        'add data in ListView
        lvDMPhieu.Items.Clear()
        Dim item As ListViewItem
        Dim i As Integer = 1
        For Each objHD As CHopDong In arrDMHopDong
            item = New ListViewItem(objHD.SOHOPDONG)
            item.SubItems.Add(objHD.NGAYKYHD.ToString(FormatDateStyle()))
            lvDMPhieu.Items.Add(item)
            i += 1
        Next
        lvDMPhieu.Focus()
        If arrDMHopDong.Count > 0 Then
            lvDMPhieu.Items(0).Selected = True
        End If
    End Sub

    Private Sub frmHopDong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create columns for the items and subitems.
        'lvDMPhieu.Columns.Add("TT", 30, HorizontalAlignment.Center)
        lvDMPhieu.Columns.Add("Số HĐ", 100, HorizontalAlignment.Center)
        lvDMPhieu.Columns.Add("Ngày ký", 75, HorizontalAlignment.Center)
        Init()
    End Sub

    Public Sub FormatGridStyleGridDMPhieu(ByVal HopDong_ID As String)
        Dim strSql As String
        Dim dsDataSet As DataSet
        Dim dv As DataView
        'strSql = "select NX, (case when(bcth = 1)then 'KH' else 'TC' end) as TT ,sophieu, (CAST(day(NGAY) AS nvarchar(2)) + '/' + cast(month(NGAY)as nvarchar(2)) + '/'+ cast(YEAR(NGAY) as nvarchar(4))) as Ngay, tendvgn, tentcnx, dbo.ChuanHoaSo(slkh) as slkh, dbo.ChuanHoaSo(slth) as slth from viewNhapXuat " & _
        '                          "where MATHANG_ID = '" & MATHANG_ID & "'"
        strSql = "select sophieu, mathang_id, ten, lo, dvt, hansd, lk, kho, tenkho, gle, sltonkh from viewNhapXuat " & _
                 " where HOPDONG = " & HopDong_ID

        dsDataSet = BaseDB.ExecSql_DataSet(strSql)
        dv = dsDataSet.Tables(0).DefaultView
        gridDMPhieu.DataSource = dv
        With gridDMPhieu
            .Columns.Item("sophieu").Frozen = True
            .Columns.Item("mathang_id").Frozen = True
            .Columns.Item("ten").Frozen = True

            .Columns.Item("mathang_id").Visible = False
            .Columns.Item("lk").Visible = False
            .Columns.Item("kho").Visible = False
            With .Columns.Item("sophieu")
                .HeaderText = "Số phiếu"
                .Width = 60
                .DefaultCellStyle.ForeColor = Color.Blue
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With

            With .Columns.Item("ten")
                .Width = 180
                .HeaderText = "Tên mặt hàng"
            End With

            With .Columns.Item("lo")
                .HeaderText = "Lô hàng"
                .Width = 100
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With

            With .Columns.Item("hansd")
                .HeaderText = "Hạn SD"
                .Width = 50
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With

            With .Columns.Item("tenkho")
                .HeaderText = "Kho"
                .Width = 70
            End With

            With .Columns.Item("dvt")
                .HeaderText = "ĐVT"
                .Width = 40
                .DefaultCellStyle.Format = FormatDateStyle()
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With

            With .Columns.Item("gle")
                .HeaderText = "Giá lẻ"
                .Width = 60
                .DefaultCellStyle.Format = FormatQuantityStyle()
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With

            With .Columns.Item("sltonkh")
                .HeaderText = "SL Tồn"
                .Width = 60
                .DefaultCellStyle.Format = FormatQuantityStyle()
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
        End With
    End Sub

    Private Sub lvDMPhieu_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvDMPhieu.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim ClickPoint As Point = New Point(e.X, e.Y)
            Dim ClickNode As ListViewItem = lvDMPhieu.GetItemAt(ClickPoint.X, ClickPoint.Y)
            ContextMenuDMPhieu.Items(0).Enabled = True
            ContextMenuDMPhieu.Items(1).Enabled = True
            ContextMenuDMPhieu.Items(2).Enabled = True
            ContextMenuDMPhieu.Items(3).Enabled = True
            ContextMenuDMPhieu.Items(4).Enabled = True
            ContextMenuDMPhieu.Items(5).Enabled = True
            If bCallForm = False Then
                ContextMenuDMPhieu.Items(4).Visible = False
                ContextMenuDMPhieu.Items(5).Visible = False
            End If
            If ClickNode Is Nothing Then
                ContextMenuDMPhieu.Items(1).Enabled = False
                ContextMenuDMPhieu.Items(2).Enabled = False
                ContextMenuDMPhieu.Items(3).Enabled = False
                ContextMenuDMPhieu.Items(4).Enabled = False
                ContextMenuDMPhieu.Items(5).Enabled = False
            End If
            ' Convert from Tree coordinates to Screen coordinates 
            Dim ScreenPoint As Point = lvDMPhieu.PointToScreen(ClickPoint)
            ' Convert from Screen coordinates to Formc coordinates 
            Dim FormPoint As Point = Me.PointToClient(ScreenPoint)
            'Show context menu 
            ContextMenuDMPhieu.Show(Me, FormPoint)
        End If
    End Sub

    Private Sub mnuItemThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemThem.Click
        Dim frm As New frmThemHopDong
        isInsert = True
        frm.fHopDongCall = Me
        frm.ShowDialog()
    End Sub

    Private Sub mnuItemXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemXoa.Click
        For Each item As ListViewItem In Me.lvDMPhieu.SelectedItems
            If BaseDB.ExecSql_DataReader("select * from tblDPNX where HOPDONG in (select hopdong_id from tblHOPDONG where sohopdong = '" + item.SubItems(0).Text + "')").Read() Then
                ShowMessages("Hợp đồng này đã được chọn trong phiếu nên không thể xóa.")
            Else
                If BaseDB.ExecSql_NoneQuery("delete from tblHOPDONG where SOHOPDONG = N'" + item.SubItems(0).Text + "'") >= 0 Then
                    Init()
                    ShowMessages("Xong.")
                End If
            End If
        Next
    End Sub

    Private Sub mnuItemChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemChon.Click
        lvDMPhieu_DoubleClick(lvDMPhieu, Nothing)
    End Sub

    Private Sub mnuItemCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemCapNhat.Click
        For Each item As ListViewItem In Me.lvDMPhieu.SelectedItems
            selectedHD = CHopDongs.GetHopDongBySoHD(item.SubItems(0).Text)
        Next
        Dim frm As New frmThemHopDong
        isInsert = False
        frm.fHopDongCall = Me
        frm.ShowDialog()
    End Sub

    Private Sub lvDMPhieu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvDMPhieu.KeyDown
        If e.KeyCode = Keys.Enter Then
            lvDMPhieu_DoubleClick(lvDMPhieu, Nothing)
        ElseIf e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub txtTimKiem_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTimKiem.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim lvItem As ListViewItem
            lvItem = lvDMPhieu.FindItemWithText(txtTimKiem.Text, True, mSelectedIndex)
            If lvItem IsNot Nothing Then
                lvDMPhieu.Focus()
                lvItem.Selected = True
                lvItem.Focused = True
                lvItem.EnsureVisible()
                mSelectedIndex = lvItem.Index + 1
                If mSelectedIndex = lvDMPhieu.Items.Count Then
                    mSelectedIndex = 0
                End If
            Else
                mSelectedIndex = 0
            End If
        End If
    End Sub

    Private Sub gridDMPhieu_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridDMPhieu.CellDoubleClick
        Dim frm As New frmQuanLyLoHang(gridDMPhieu.Rows(gridDMPhieu.SelectedCells(0).RowIndex).Cells("lk").Value.ToString(), gridDMPhieu.Rows(gridDMPhieu.SelectedCells(0).RowIndex).Cells("kho").Value.ToString(), CInt(gridDMPhieu.Rows(gridDMPhieu.SelectedCells(0).RowIndex).Cells("mathang_id").Value))
        frm.ShowDialog(Me)
    End Sub

    Private Sub gridDMPhieu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles gridDMPhieu.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub
End Class